const jwt = require('jwt-simple')
const baseConfig = require('../base-config')

const secret = 'shankeshankeshanke'

function genToken(passkey) {
    const payload = { passkey: passkey }
    
    // HS256 secrets are typically 128-bit random strings, for example hex-encoded:
    // var secret = Buffer.from('fe1a1915a379f3be5394b64d14794932', 'hex')
    
    // encode
    const token = jwt.encode(payload, secret)

    return token
}



function verifyToken(token) {
    try {
        const decoded = jwt.decode(token, secret);
        if (decoded && decoded.passkey) {
            if (!baseConfig.passkey || decoded.passkey === baseConfig.passkey) {
                return true
            }
        }
    } catch (error) {
    }
    return false
}

module.exports = {
    genToken,
    verifyToken
}